import { getEntSpaceId, setEntSpaceId } from '@/utils/auth'
import { getMenu } from '@/api/user'
import { postHandle } from '@/api'

const state = {
  obj: {},
  entSpaceId: getEntSpaceId(),
  menuList: []
}

const mutations = {
  SET_ENTSPACEID(state, entSpaceId) {
    setEntSpaceId(entSpaceId)
    state.entSpaceId = entSpaceId
  },
  SET_ENT_OBJ(state, obj) {
    state.obj = obj
  },
  SET_MENUSLIST: (state, menuList) => {
    state.menuList = menuList
  }
}

const actions = {
  getObject({ commit, state }) {
    return new Promise((resolve, reject) => {
      postHandle(`/api/v1/welder-sbtx-second/enterprise-comb/ent-by-spaceid/${state.entSpaceId}`, {}).then(res => {
        commit('SET_ENT_OBJ', res.result)
        resolve(res.result)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getMenuList({ state, commit }) {
    return new Promise((resolve, reject) => {
      getMenu({
        spaceId: state.entSpaceId,
        applySceneCode: 'webAdminMenu',
        typeCode: 'businessMenu',
        businessCode: 'enterpriseMenu'
      }).then(response => {
        const data = response.result
        commit('SET_MENUSLIST', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
